$(function() {
	// 获取todo
	function getData(str) {
		var data = localStorage.getItem(str);
		if (data != null) {
			return (JSON.parse(data));
		} else {
			return [];
		}
	}
	// 设置todo
	function setData(str, data) {
		localStorage.setItem(str, JSON.stringify(data));
	}

	// 页面加载
	function loadHtml() {
		var data = getData("todolist");
		var deletedData = getData("deletedList");
		var todoCount = 0;
		var doneCount = 0;
		var deletedCount = 0;
		$("ul").empty();
		$(".title div").empty();
		$(data).each(function(index, element) {
			if (element.done) {
				$("#done").prepend('<li><a href="javascript:" index="' + index +
					'" title="移除标签"></a><p>' + element.title +
					'<span>' + element.time + '</span></p><input type="checkbox" checked/></li>');
				doneCount++;
			} else {
				$("#doing").prepend('<li><a  href="javascript:" index="' + index +
					'" title="移除标签"></a><p>' + element.title +
					'<span>' + element.time + '</span></p><input type="checkbox"/></li>');
				todoCount++;
			}
		})
		$(deletedData).each(function(index, element) {
			$("#deleted").prepend('<li index="' + index + '"><p>' + element.title + '<span>' + element
				.time +
				'</span></p><span>想做了</span></li>');
			deletedCount++;
		})
		$(".doing .title div").text(todoCount);
		$(".done .title div").text(doneCount);
		$(".recover .title div").text(deletedCount);
	}
	// 时间函数
	function time() {
		function add0(obj) {
			return obj < 10 ? ('0' + obj) : obj;
		}
		var myDate = new Date();
		var year = myDate.getFullYear();
		var month = myDate.getMonth() + 1;
		var date = myDate.getDate();
		var hour = myDate.getHours();
		var minute = myDate.getMinutes();
		var second = myDate.getSeconds();
		var str = '' + add0(year) + "\\" + add0(month) + "\\" + add0(date) + "\\ " + add0(hour) + "\:" + add0(
			minute) + "\:" + add0(second);
		return str;
	}
	//删除函数
	function deletedList(data) {
		var deleted = getData("deletedList");
		deleted.push(data.splice($(this).attr("index"), 1)[0]);
		setData("deletedList", deleted);
		return data;
	}



	loadHtml();
	// 清空列表
	$(".moyu").on("click", function() {
		if (confirm("注意：这将会清空所有本地内存！")) {
			localStorage.removeItem("todolist");
			localStorage.removeItem("deletedList");
			loadHtml();
		}
	})
	// 添加标签
	$("#text").on("keydown", function(event) {
		var local = getData("todolist");
		if ($(this).val() === '') return;
		if (event.keyCode === 13) {
			local.push({
				title: $(this).val(),
				done: false,
				time: time()
			})
			setData("todolist", local);
			loadHtml();
			$(this).val("");
		}
	})
	// 删除标签
	$("ul").on("click", "a", function() {
		var data = getData("todolist");
		var newD = deletedList.call(this, data);
		setData("todolist", newD);
		loadHtml();
	})
	// 栏位切换
	$("ul").on("click", "input", function() {
		var data = getData("todolist");
		data[$(this).siblings("a").attr("index")].done = $(this).prop("checked");
		setData("todolist", data);
		loadHtml();
	})

	//恢复列表切换
	$(".recover").on("click", ".btn", function() {
		if ($(this).html() == '&lt;') {
			$(this).html("&gt;").parent().stop().animate({
				right: 0
			});
		} else {
			$(this).html("&lt;").parent().stop().animate({
				right: "-25rem"
			});
		}
	})
	// 恢复列表
	$("#deleted").on("click", "span", function(event) {
		var todoList = getData("todolist");
		var deletedList = getData("deletedList");
		todoList.push(deletedList.splice($(event.toElement).parents("li").attr("index"), 1)[0]);
		setData("todolist", todoList);
		setData("deletedList", deletedList);
		loadHtml();
	})
})
